package algorithmTopics.queue;

import java.util.*;

public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int lineNum = sc.nextInt();
        sc.nextLine();
        //用于存储要打印的文件的打印机的名字
        Map<String,PriorityQueue<bigHelper<String,String>>> prints = new HashMap<>();
        for (int i = 0; i < lineNum; i++) {
            //一行一行读取
            String[] line = sc.nextLine().split(" ");
            //In 添加打印机和文件
            if (line[0].equals("IN")) {
                //打印机名字
                String printName = line[1];
                if (prints.get(printName) == null) {
                  //有文件要打印 ,存放文件名字和优先级
                  bigHelper<String,String> file = new bigHelper(i + 1,line[2]);
                  //大顶堆来维护优先级
                  PriorityQueue<bigHelper<String,String>> files =
                  new PriorityQueue<>(new Comparator<bigHelper<String, String>>() {
                      @Override
                      public int compare(bigHelper<String, String> o1, bigHelper<String, String> o2) {
                          if (o2.getV().equals(o1.getV())) {
                              return Integer.parseInt(o1.getK()) - Integer.parseInt(o2.getK());
                          }
                          return Integer.parseInt(o2.getV()) - Integer.parseInt(o1.getV());
                      }
                  });
                  files.add(file);
                  prints.put(printName, files);
                }else{
                    // 打印机有了,添加新的文件
                    PriorityQueue<bigHelper<String, String>> files = prints.get(printName);
                    files.add(new bigHelper(i+1, line[2]));
                }

            }else{
                //out 要输出文件
                //打印机名字
                String printName = line[1];
                //里面的文件
                PriorityQueue<bigHelper<String, String>> files = prints.get(printName);
                if (files.size() == 0) {
                    System.out.println("NULL");
                }else{
                    bigHelper<String, String> poll = files.poll();
                    System.out.println(String.valueOf(poll.getK()));
                }
            }

        }
        sc.close();

    }



    static class bigHelper<K,V>{
        // k是文件名字，v 是优先级
        K k;
        V v;
        public bigHelper(K k ,V v){
            this.k = k;
            this.v = v;
        }
        K getK(){
            return this.k;
        }
        V getV(){
            return this.v;
        }
    }
}